<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <link href="../../static/layui/css/layui.css" rel="stylesheet">
    <script src="../../static/layui/layui.js"></script>
    <style>
        .container{
            padding: 25px;
        }
    </style>
</head>
<body>
<!--行工具栏模板-->
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">删除</a>
    </div>
</script>
<!--头部工具栏模板-->
<script type="text/html" id="userBar">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-sm" lay-event="add">新增 </a>
        <a class="layui-btn layui-btn-sm layui-bg-red" lay-event="batchDelete">批量删除 </a>
    </div>
</script>

<div class="container">
    <div class="search">
        <form class="layui-form userForm layui-row layui-col-space16">
            <div class="layui-col-md4">
                <div class="layui-input-wrap">
                    <input type="text" name="courseType" value="" placeholder="请输入课程类型" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-col-md4">
                <button class="layui-btn" lay-submit lay-filter="table-search">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </form>
    </div>
    <table class="layui-hide" id="ID-table-demo-data" lay-filter="courseType-filter"></table>
</div>
<script>
    layui.use(function(){
        // 获取表格对象
        let table = layui.table,
            form = layui.form,
            $ = layui.$,
        layer = layui.layer;
        // 已知数据渲染
        let inst = table.render({
            elem: '#ID-table-demo-data',
            id:'courseType',
            url:"/ssm/courseType/page",
            toolbar: '#userBar',
            cols: [[ //标题栏
                {type: 'checkbox', fixed: 'left'},   // 设置复选框
                {field: 'type', title: '课程类型'},
                {field: 'createTime', title: '创建时间'},
                {fixed: 'right', title:'操作', width: 150, toolbar: '#barDemo'}
            ]],
            skin: 'line', // 表格风格
            even: true,
            page: true, // 是否显示分页
            limits: [5, 10, 15],
            limit: 5 // 每页默认显示的数量
        });

        table.on('toolbar(courseType-filter)', function(obj) {
            let id = obj.config.id;
            let checkStatus = table.checkStatus(id);
            let infos = checkStatus.data

            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        area: ['30%','40%'],
                        shade: 0.6,
                        shadeClose: true,
                        anim: 0,
                        end: function(){
                            table.reload("courseType")
                        },
                        content: 'add.html'
                    });
                    break;
                case 'batchDelete':
                    let ids = infos.map(info=>info.id);
                    if (ids.length==0){
                        layer.msg("请至少选择一条数据")
                        return;
                    }
                    $.ajax({
                        url:'/ssm/courseType/delete',
                        data: {ids},
                        traditional:true,
                        type:"get",
                        dataType:'json',
                        success:function (res){
                            if (res.code==0){  //  删除成功
                                layer.msg(res.message,{icon:1},function (){
                                    table.reload("courseType",{
                                        where:{
                                            page:1,
                                            limit:5
                                        }
                                    })  //  表格数据的重载
                                })
                            }else{
                                layer.msg(res.message,{icon:2})
                            }
                        }
                    })
                    break;
            }
            ;
        });

        // 触发单元格工具事件
        table.on('tool(courseType-filter)', function(obj){ // 双击 toolDouble
            let data = obj.data;
            if(obj.event === 'edit'){
                $.get('/ssm/courseType/getById',{id:data.id},function (res){
                    if (res.code==0){
                        // 获取用户信息
                        let courseType = res.data
                        sessionStorage.setItem("courseType",JSON.stringify(courseType))
                        layer.open({
                            title: '编辑',
                            type: 2,
                            area: ['30%','40%'],
                            end:function (){
                                table.reload("courseType")
                            },
                            content: 'edit.html'
                        });
                    }else{
                        layer.msg(res.message,function (){
                            table.reload("courseType")
                        })
                    }
                })
            } else if(obj.event === 'delete'){
                let  id = data.id;
                //  发送异步请求
                $.ajax({
                    url:'/ssm/courseType/delete',
                    data: {ids:id} , //  {ids:ids}    es6   /sport/user/delete?ids=1
                    type:"get",
                    dataType:'json',
                    success:function (res){
                        if (res.code==0){  //  删除成功
                            layer.msg(res.message,{icon:1},function (){
                                table.reload("courseType",{
                                    page:{
                                        curr:1,
                                        limit:5
                                    }
                                })  //  表格数据的重载
                            })
                        }else{
                            layer.msg(res.message,{icon:2})
                        }
                    }
                })
            }

        });

        // 监听表单事件
        form.on('submit(table-search)',function (obj){
            let  searchData = obj.field;
            //  重载表格数据
            table.reload('courseType', {
                where: {
                    courseType:searchData.courseType
                }
            });
            return false;
        })
    });
</script>

</body>
</html>
